package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.zabbix.Zabbix;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 监控 触发器
 * Created by xf on 2017/3/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/trigger")
public class TriggerController {


    /**
     * 修改触发器的属性
     * @param triggerId 触发器id
     */
    @RequestMapping(value = "/update/{triggerId}", method = {RequestMethod.POST})
    public List<String> update(@PathVariable String triggerId, @RequestParam("attributeNames") String attributeNames,
                                                     @RequestParam("attributeValues") String attributeValues){
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("triggerId",triggerId);
        String[] names = attributeNames.trim().split(",");
        String[] values = attributeValues.trim().split(",");
        for (int i = 0; i < names.length; i++) {
            params.put(names[i], values[i]);
        }
        Map<String, Object> resp = zabbix.call("trigger.update", params);
        Map<String, Object> result = (Map<String, Object>) resp.get("result");
        List<String> res = (List<String>) result.get("triggerids");
        return res;
    }

    /**
     * 根据触发器id的详细信息
     * @param triggerId 触发器id
     */
    @RequestMapping(value = "/get/{triggerId}", method = {RequestMethod.POST})
    public List<Map<String,Object>> get(@PathVariable String triggerId){
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("triggerids",triggerId);
        params.put("output","extend");
        params.put("selectFunctions","extend");
        Map<String, Object> resp = zabbix.call("trigger.get", params);
        List<Map<String,Object>> result = (List<Map<String,Object>>) resp.get("result");
        return result;
    }




}
